/*********************************************************************
 * Clase de utilidad. Actor Manager 
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package graphics;

import util.ScoreBoard;
import aze.display.TileLayer;
import graphics.sprites.Player;
import graphics.sprites.NonPlayer;
import graphics.sprites.Prize;
import graphics.sprites.Enemy;
import nme.events.KeyboardEvent;
import nme.events.MouseEvent;


class ActorManager {
  var _root:TileLayer;            // Capa de dibujado
  var _w:Int;  var _h:Int;        // Ancho y alto de _root
  var _player:Player;             // Objeto de jugador Pcpal. 
  var _vPrize:Array<NonPlayer>;   // Array de estrellas activas
  var _vEnemy:Array<NonPlayer>;   // Array de enemigos activos
  var _scoreBoard:ScoreBoard;     // Objeto para actualizar puntuacion
  
  inline static var PROBPRIZE:Int = 10;   // Probabilidad de Estrella
  inline static var PROBENEMY:Int = 10;   // Probabilidad de Enemigo

  // Constructor =====================================================
  public function new(layer:TileLayer, w:Int, h:Int, sb:ScoreBoard) {
    _root = layer; _w = w; _h = h; _scoreBoard = sb;
    _vPrize = new Array<NonPlayer>();
    _vEnemy = new Array<NonPlayer>();
  }

  // Añadir Elementos ================================================
  public function addPlayer(id:String) {
    _player = new Player(_root, id, _scoreBoard, _vPrize, _vEnemy);
  }

  function addPrize(id:String) {
    var p = new Prize(id, _root,  _w, _h, 127, 127, Std.random(10));
    _root.addChild(p);  _vPrize.push(p);
  }

  function addEnemy(id:String) {
    var p = new Enemy(id, _root,  _w, _h, 127, 79, 5 + Std.random(10));
    _root.addChild(p);  _vEnemy.push(p);
  }

  // Update ==========================================================
  function updateNonPlayer(v:Array<NonPlayer>, eTime:Int):Void {
    for (p in v) 
      if (p.update(_w, _h, eTime) == false) {
	_root.removeChild(p);   v.remove(p);
      }
  }

  public function update(w:Int, h:Int, eTime:Int):Void {
    _player.update(w,h,eTime); _w = w; _h = h;
    if (Std.random(1000) < PROBPRIZE) addPrize("star");
    if (Std.random(1000) < PROBENEMY) addEnemy("enemy");
    if (Std.random(1000) < PROBENEMY && Std.random(1000) % 2 == 0) addEnemy("gomes");
    updateNonPlayer(_vPrize, eTime);
    updateNonPlayer(_vEnemy, eTime);
  }

  public function onKeyPressed(event:KeyboardEvent):Void {
    _player.onKeyPressed(event);
  }

  public function onMouseMove(event:MouseEvent):Void {
    _player.onMouseMove(event);
  }

}
